home *** CD-ROM | disk | FTP | other *** search
/ MacWorld 1997 August / Macworld (1997-08).dmg / Shareware World / Info / For Developers / SC Basic / Example Programs / File Info / FileInfo.bas < prev    next >
BASIC Source File  |  1997-06-17  |  9KB  |  270 lines

  1. Global EventRecord As Structure
  2.         Global event_what As Word
  3.         Global event_message As Integer
  4.         Global event_when As Integer
  5.         Global event_where As Integer
  6.         Global event_modifiers As Word
  7. Endstruct
  8.  
  9. Global DialogPtr As Integer
  10.  
  11. * Main entry point
  12. Do Initialise
  13. Do Process_File
  14. Do Event_Loop
  15. End
  16.  
  17. *********************************************************
  18. *                                                                                                                                                                                                                            *
  19. *    Initialise                                                                                           *
  20. *********************************************************
  21. Procedure Initialise()
  22. Local Gash As Integer
  23.         * GetAppCount returns the number os files dropped onto this
  24.         * application. Reject if none dropped.
  25.     If GetAppCount()=0
  26.                 Gash=MsgBox("Drag a file onto the"+Chr(13)+"Fileinfo application","Ok","")
  27.         End
  28.     Endif
  29.         * Create dialog box from DLOG resource 150
  30.     DialogPtr=_GetNewDialog(Word(150),0,-1)   
  31. Return
  32.  
  33. *********************************************************
  34. *                                                                                                              *
  35. *    Event Loop                                                                                          *
  36. *********************************************************
  37. Procedure Event_Loop()
  38. Local Done As Integer
  39. Local gotEvent As Byte
  40.  
  41.     Done=0
  42.     Repeat
  43.         * Wait for an operating system event to occur
  44.         gotEvent=_WaitNextEvent(Word(-1),EventRecord,0,0)
  45.         * ignore null events
  46.         If Integer(gotEvent)<>0 Then
  47.             Do DoEvent(Byref(Done))
  48.         Endif
  49.     Until Done
  50. Return
  51.  
  52. *********************************************************
  53. *                                                                                                              *
  54. *    Do Event                                                                                             *
  55. *********************************************************
  56. Procedure DoEvent(Done)
  57. Parameter Done As Integer Byref
  58. Local what As Integer
  59.  
  60.     what=Integer(event_what)
  61.     Do Case
  62.         Case what=1    'mouseDown
  63.             Do DoMouseDown(Byref(Done))
  64.             Break
  65.         Case what=6 'updateEvt
  66.             Do UpdateEvent
  67.         Endcase
  68. Return
  69.  
  70. *********************************************************
  71. *                                                                                                              *
  72. *    Mouse Down                                                                                        *
  73. *********************************************************
  74. Procedure DoMouseDown(Done)
  75. Parameter Done As Integer Byref
  76. Local part As Integer
  77. Local thisWindow As Integer
  78.     
  79.     part=Integer(_FindWindow(event_where,Varptr(thisWindow)))
  80.     Do Case
  81.         Case part=3 'inContent    
  82.             Do MouseDownInWindow(thisWindow,Byref(Done))    
  83.         Case part=4 'inDrag    
  84.             Do DragWindow(thisWindow)    
  85.     Endcase
  86. Return
  87.  
  88. *********************************************************
  89. *                                                                                                              *
  90. *    Drag Window                                                                                      *
  91. *********************************************************
  92. Procedure DragWindow(thisWindow)
  93. Parameter thisWindow As Integer
  94. Local GrayRgnHandle As Integer
  95.  
  96.     If thisWindow = _FrontWindow() Then
  97.         GrayRgnHandle=Lpeek(&H9EE) ' Global variable
  98.         _DragWindow(thisWindow,event_where,Lpeek(GrayRgnHandle)+2)
  99.     Endif
  100. Return
  101.  
  102. *********************************************************
  103. *                                                                                                              *
  104. *    Update Event                                                                                      *
  105. *********************************************************
  106. Procedure UpdateEvent()
  107. Local windowPtr As Integer
  108.         
  109.     windowPtr=event_message
  110.     _SetPort(windowPtr)
  111.     _BeginUpdate(windowPtr)
  112.     _DrawDialog(windowPtr)
  113.         _UpdateControls(windowPtr,Lpeek(windowPtr+24))        ' visRgn
  114.     _EndUpdate(windowPtr)
  115.  
  116. Return
  117.  
  118. *********************************************************
  119. *                                                                                                              *
  120. *     Mouse Down in Window                                                                     *
  121. *********************************************************
  122. Procedure MouseDownInWindow(thisWindow,Done)
  123. Parameter thisWindow As Integer
  124. Parameter Done As Integer Byref
  125. Local windowType As Integer
  126. Local DialogPtr As Integer
  127. Local itemHit As Word
  128. Local Ret As Byte
  129.  
  130.     If thisWindow <> _FrontWindow() Then
  131.         * Make this window active if it is not already
  132.         _SelectWindow(thisWindow)
  133.     Else
  134.         * Find out if mouse down was in a control and if so which one
  135.         Ret=_DialogSelect(EventRecord,Varptr(DialogPtr),Varptr(itemHit))
  136.         If Integer(Ret)=1 
  137.             If Integer(itemHit)=5 ' the Button
  138.                 Done=-1
  139.             Endif
  140.         Endif
  141.     Endif
  142. Return
  143.  
  144. *********************************************************
  145. *                                                                                                              *
  146. *        Process File                                                                                   *
  147. *********************************************************
  148. Procedure Process_File()
  149. Local Command As Integer
  150.  
  151. Local AppStruct As Structure
  152.     Local AvRefNum As Word
  153.     Local Atype As Integer
  154.     Local AversNum As Byte
  155.     Local Afiller As Byte
  156.     Local AfileName As Str255 [64]
  157. Endstruct
  158.  
  159.     * find out if we have been asked to open a file or print a file
  160.     Command=GetAppMessage()
  161.  
  162.     Do Case
  163.         Case Command=0 ' Open file
  164.             * get details of the file to open (first one only)
  165.                     AppStruct=GetAppFile(1)
  166.                         Do Show_File_Details(Integer(AvRefNum),String(AfileName))
  167.                      Break
  168.             Case Command=1 ' Print file
  169.             * reject as this program does not print files
  170.             Print "FileInfo does not print files"
  171.             Inkey
  172.         Endcase
  173.  
  174. Return
  175.  
  176. *********************************************************
  177. *                                                                                                              *
  178. *        Show File Details                                                                           *
  179. *********************************************************
  180. Procedure Show_File_Details(RefNum,fileName)
  181. Parameter RefNum As Integer
  182. Parameter fileName As String
  183. Local Ret As Word
  184. Local fName As Str255
  185.  
  186. Local paramBlock As Structure
  187.     Local Filler1 As Char [12]
  188.     Local ioCompletion As Integer
  189.     Local ioResult As Word
  190.     Local ioNamePtr As Integer
  191.     Local ioVRefNum As Word
  192.     Local ioFRefNum As Word
  193.     Local ioFVersNum As Byte
  194.     Local Filler2 As Byte
  195.     Local ioFDirIndex As Word
  196.     Local ioFlAttrib As Byte
  197.     Local Filler3 As Byte
  198.     Local pFInfo As Char [16]
  199.     Local ioDirID As Integer
  200.     Local Filler4 As Char [56]
  201. Endstruct
  202.  
  203. Local FInfo As Structure
  204.     Local fdType as Integer
  205.     Local fdCreator As Integer
  206.     Local fdFlags As Word
  207.     Local fdLocation As Integer
  208.     Local fdFldr As Word
  209. Endstruct
  210.  
  211. Local UserItemType As Word
  212. Local DialogItem As Integer
  213.  
  214. Local UserItemRect As Structure
  215.         Local R1 As Word
  216.         Local R2 As Word
  217.         Local R3 As Word
  218.         Local R4 As Word
  219. Endstruct
  220.  
  221.         * Set up paramBlock for PBGetCatInfo call
  222.     ioCompletion=0
  223.     fName=Str255(fileName)
  224.     ioNamePtr=Varptr(fName)
  225.     ioVRefNum=Word(RefNum)
  226.     ioFDirIndex=0
  227.     ioDirID=0
  228.  
  229.     Ret=_PBGetCatInfo(paramBlock)
  230.  
  231.     If Integer(Ret)=0
  232.         FInfo=pFInfo
  233.  
  234.         *    Get Handle to Dialog Item 3.    (File type box) 
  235.         UserItemType=Word(16) ' Editable Text
  236.         DialogItem=0 ' Dont know why we have to set this
  237.       _GetDialogItem(DialogPtr,Word(3),Varptr(UserItemType),Varptr(DialogItem),UserItemRect)
  238.         * Set Dialog Item 3 to File Type string
  239.             _SetDialogItemText(DialogItem,Str255(IntToChars(fdType)))
  240.  
  241.        *    Get Handle to Dialog Item 4.    (Creator box) 
  242.         UserItemType=Word(16)
  243.         DialogItem=0
  244.       _GetDialogItem(DialogPtr,Word(4),Varptr(UserItemType),Varptr(DialogItem),UserItemRect)
  245.         * Set Dialog Item 4 to Creator string
  246.             _SetDialogItemText(DialogItem,Str255(IntToChars(fdCreator)))
  247.     Else
  248.         Print "Error in PBGetCatInfo ";Ret
  249.         Inkey
  250.     Endif
  251. Return
  252.   
  253. *********************************************************
  254. *                                                                                                              *
  255. *       Convert Integer to Chars                                                                 *
  256. *********************************************************  
  257. Function IntToChars(InInt) Returning String
  258. Parameter InInt As Integer
  259. Local I As Integer
  260. Local ChString As String [4]
  261.  
  262.         * File types and Creator types are stored as a 32 bit integer with 4
  263.         * characters encoded within each byte. Use peek and carptr to get each byte.
  264.     ChString=""
  265.     For I=1 to 4
  266.         ChString=ChString+Chr(Peek(Varptr(InInt)+I-1))
  267.     Next I
  268.  
  269. Return ChString
  270.